package com.scraper.articles.web.controller;

import com.scraper.articles.web.model.Article;
import com.scraper.articles.web.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping(value = {"/listArticles/{requestId}"})
    public String listArticles(@PathVariable Long requestId, Map<String, Object> map) {

        map.put("articlesList", articleService.listArticles(requestId));

        return "/article/listArticles";
    }

    @RequestMapping(value = {"/delete/{articleId}"})
    public String deleteArticle(@PathVariable Long articleId) {
        Article article = articleService.getArticle(articleId);

        articleService.deleteArticle(articleId);

        return "redirect:/article/listArticles/" + article.getRequestId();
    }
}
